package com.supreme.uploadserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.uploadserver.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "mail")
public class EmailController {

    @Resource
    private EmailService emailService;

    @GetMapping(value = "test")
    public AjaxResp test() {
        final JSONObject jsonObject = emailService.test();
        return AjaxResp.instance(jsonObject);
    }

    @GetMapping(value = "getMessageList")
    public AjaxResp getMessageList(@RequestParam(value = "lastCount", defaultValue = "0") Integer lastCount) {
        List<JSONObject> emailList = emailService.emailSyncInboxFolder(lastCount);
        return AjaxResp.instance(emailList);
    }

    @PostMapping(value = "sendTextMail")
    public AjaxResp sendTextMail(@RequestBody JSONObject bodyData) {
        String mailTo = bodyData.getString("mailTo");
        String subject = bodyData.getString("subject");
        String content = bodyData.getString("content");
        emailService.sendTextMail(mailTo, subject, content);
        return AjaxResp.instance();
    }

    @PostMapping(value = "sendHtmlMail")
    public AjaxResp sendHtmlMail(@RequestBody JSONObject bodyData) {
        String mailTo = bodyData.getString("mailTo");
        String subject = bodyData.getString("subject");
//        String content = bodyData.getString("content");
        String content = "<!DOCTYPE html><html>\n" +
                "<head><meta charset=\"utf-8\"/><script src=\"http://code.jquery.com/jquery-latest.min.js\"></script></head>\n" +
                "<body>\n" +
                "<div id=\"wrap\"><div id=\"header\"><h1>html在线工具</h1>\n" +
                "<!--   如果有用，请别忘了推荐给你的朋友：\t\t-->\n" +
                "<!--   Html在线美化、格式化：https://tool.lu/html   -->\n" +
                "</div>\n" +
                "<div id=\"main\">\n" +
                "\t<!-- [history] -->\n" +
                "\t<dl>\n" +
                "\t<dt>v1.0</dt> <dd>2011-06-05 Html工具上线</dd>\n" +
                "\t<dt>v1.1</dt> <dd>2012-01-14 修复美化功能，增加压缩</dd>\n" +
                "\t<dt>v1.2</dt> <dd>2012-07-20 增加清除链接功能</dd>\n" +
                "\t<dt>v1.3</dt> <dd>2014-08-05 修改 html 压缩引擎</dd>\n" +
                "\t<dt>v1.4</dt> <dd>2014-08-09 增加转换为js变量的功能</dd>\n" +
                "\t<dt>v1.5</dt> <dd>2020-11-21 增加内联css功能</dd>\n" +
                "\t</dl>\n" +
                "</div>\n" +
                "<div id=\"footer\">This is just an example.</div>\n" +
                "</div>\n" +
                "</body></html>";
        try {
            emailService.sendHtmlMail(mailTo, subject, content);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return AjaxResp.instance();
    }
}
